package com.niit.Hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

public class TableAmdinTest2 {
    private Configuration configuration;
    private Connection connection;
    private Admin admin;

    @Before
    public void beforeTest() throws IOException{
        //使用HbaseConfiguration.create()创建Hbase配置
        configuration = HBaseConfiguration.create();
        //使用ConnectionFactory.createConnection()创建Hbase连接
        connection = ConnectionFactory.createConnection(configuration);
        //要创建表，需要基于Hbase连接获取admin管理对象
        admin = connection.getAdmin();
    }

    //使用admin.close、connection.close关闭连接
    @After
    public  void afteerTest() throws IOException{
        admin.close();
        connection.close();
    }

    //创建命名空间
    @Test
    public void creatNamespace(){
        //1.调用方法创命名空间
        NamespaceDescriptor.Builder builder = NamespaceDescriptor.create("my_hbase");
        //2.使用builder构造出对应的添加完参数的对象完成创建
        //创建命名空间出现的问题，都属于自身的问题，不应该抛出
        try{
            admin.createNamespace(builder.build());
        } catch (IOException e) {
            System.out.println("命名空间已经存在");
            e.printStackTrace();
        }
    }

    //判断表是否存在
    @Test
    public void isTableExists(){
        boolean b = false;
        try {
            b = admin.tableExists(TableName.valueOf("student"));
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    //创建表
    @Test
    public void creatTable() throws IOException{
        //名称空间名
        String NAME_SPACE = "my_hbase";
        //表名
        String TABLE_NAME = "student";
        //列簇名
        String COLUME_FAMIY = "c1";

        //1.判断表是否存在
        if (admin.tableExists(TableName.valueOf(NAME_SPACE,TABLE_NAME))){
            return;
        }

        //2.构建表描述构建器
        TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf(NAME_SPACE,TABLE_NAME));

        //3.构建列簇描述构建器
        ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(COLUME_FAMIY));

        //构建列簇描述
        ColumnFamilyDescriptor columnFamilyDescriptor = columnFamilyDescriptorBuilder.build();

        //构建表的描述
        //添加列簇
        tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor);
        TableDescriptor tableDescriptor = tableDescriptorBuilder.build();
        //6.创建表
        admin.createTable(tableDescriptor);
    }

    //修改表
    @Test
    public void modifyTable() throws IOException{
        //名称空间名
        String NAME_SPACE = "my_hbase";
        //表名
        String TABLE_NAME = "student";
        //列簇名
        String COLUMN_FAMILY = "c1";
        int VERSION = 2;

    }

}
